package com.ruoyi.web.controller.teaching;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.Ztree;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.teaching.domain.*;
import com.ruoyi.teaching.service.*;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 快速备课Controller
 *
 * @author ruoyi
 * @date 2022-10-12
 */
@Controller
@RequestMapping("/teaching/prepare")
public class TeachingClassesPrepareController extends BaseController {
    private String prefix = "teaching/prepare";

    @Autowired
    private ITeachingClassesService teachingClassesService;

    @Autowired
    private ITeachingStudentApplyService teachingStudentApplyService;

    @Autowired
    private ITeachingCourseService teachingCourseService;

    @Autowired
    private ITeachingCourseFolderService teachingCourseFolderService;
    @Autowired
    private ITeachingCourseWareService teachingCourseWareService;

    @Autowired
    private ITeachingClassesScheduleService scheduleService;

    @GetMapping()
    public String prepare() {
        return prefix + "/prepare";
    }

    /**
     * 查询教务班级列表
     */
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(TeachingClasses teachingClasses) {
        startPage();
        //找到当前老师需要上的班级
        List<TeachingClasses> list = teachingClassesService.selectTeachingClassesByTeacherId(getUserId());
        return getDataTable(list);
    }

    /**
     * 导出教务班级列表
     */
    @Log(title = "教务班级", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(TeachingClasses teachingClasses) {
        List<TeachingClasses> list = teachingClassesService.selectTeachingClassesList(teachingClasses);
        ExcelUtil<TeachingClasses> util = new ExcelUtil<TeachingClasses>(TeachingClasses.class);
        return util.exportExcel(list, "教务班级数据");
    }

    /**
     * 新增教务班级
     */
    @GetMapping("/add")
    public String add(ModelMap mmap) {
        mmap.put("courses", teachingCourseService.selectTeachingCourseAll());
        return prefix + "/add";
    }

    /**
     * 新增保存教务班级
     */
    @Log(title = "教务班级", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(TeachingClasses teachingClasses) {
        teachingClasses.setCreateId(getUserId());
        teachingClasses.setCreateBy(getLoginName());
        teachingClasses.setCreateTime(new Date());
        return toAjax(teachingClassesService.insertTeachingClasses(teachingClasses));
    }

    /**
     * 修改教务班级
     */
    @GetMapping("/edit/{classId}")
    public String edit(@PathVariable("classId") Long classId, ModelMap mmap) {
        TeachingClasses teachingClasses = teachingClassesService.selectTeachingClassesByClassId(classId);
        mmap.put("teachingClasses", teachingClasses);
        return prefix + "/edit";
    }

    /**
     * 修改保存教务班级
     */
    @Log(title = "教务班级", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(TeachingClasses teachingClasses) {
        teachingClasses.setUpdateBy(getLoginName());
        teachingClasses.setUpdateTime(new Date());
        return toAjax(teachingClassesService.updateTeachingClasses(teachingClasses));
    }

    /**
     * 删除教务班级
     */
    @Log(title = "教务班级", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(teachingClassesService.deleteTeachingClassesByClassIds(ids));
    }

    /**
     * 班级详情
     */
    @GetMapping("/info/{classId}")
    public String Info(@PathVariable("classId") Long classId, ModelMap mmap) {
        TeachingClasses teachingClasses = teachingClassesService.selectTeachingClassesByClassId(classId);
        mmap.put("teachingClasses", teachingClasses);
        return prefix + "/info";
    }

    /**
     * 三级联动
     *
     * @return
     */
    @GetMapping("/select")
    public String select() {
        return prefix + "/select";
    }

    /**
     * 添加学生进班级
     */
    @GetMapping("/addStudent/{classId}")
    public String addStudent(@PathVariable("classId") Long classId, ModelMap mmap) {
        TeachingClasses teachingClasses = teachingClassesService.selectTeachingClassesByClassId(classId);
        mmap.put("teachingClasses", teachingClasses);
        return prefix + "/addStudent";
    }

    @GetMapping("/detail/{classId}")
    public String detail(@PathVariable("classId") Long classId, ModelMap mmap) {
        TeachingClasses classes = teachingClassesService.selectTeachingClassesByClassId(classId);
        mmap.put("classes", classes);
        return prefix + "/detail";
    }

    /**
     * 加载课程目录树列表
     */
    @GetMapping("/treeData")
    @ResponseBody
    public List<Ztree> treeData(@RequestParam("classId") Long classId) {
        TeachingClasses classes = teachingClassesService.selectTeachingClassesByClassId(classId);
        Long courseId = classes.getCourseId();
        TeachingCourseFolder folder = new TeachingCourseFolder();
        if (StringUtils.isNotNull(courseId)) {
            folder.setCourseId(courseId);
        }
        //并且id必须都存在 班级已上课程的ID里
        String ancestors = classes.getCurrentFolderAncestors();
        Map<String, Object> param = new HashMap<>();
        param.put("ancestors", ancestors.split(","));
        folder.setParams(param);
        List<Ztree> ztrees = teachingCourseFolderService.selectTeachingCourseFolderTree(folder);

        return ztrees;
    }


    /**
     * 课件列表
     *
     * @param teachingCourseWare
     * @return
     */
    @PostMapping("/ware/list/{classId}")
    @ResponseBody
    public TableDataInfo list(@PathVariable("classId") Long classId, TeachingCourseWare teachingCourseWare) {
        startPage();
        TeachingClasses classes = teachingClassesService.selectTeachingClassesByClassId(classId);
        //并且id必须都存在 班级已上课程的ID里
        teachingCourseWare.setStatus("0");
        String ancestors = classes.getCurrentFolderAncestors();
        Map<String, Object> param = new HashMap<>();
        param.put("ancestors", ancestors.split(","));

        if (classes.getLessonStatus().equals("1")) {
            param.put("currentFolderId", classes.getCurrentFolderId());
        }
        teachingCourseWare.setParams(param);
        List<TeachingCourseWare> list = teachingCourseWareService.selectTeachingCourseWareList(teachingCourseWare);
        return getDataTable(list);
    }

    @GetMapping("/preLesson/{classId}")
    public String preLesson(@PathVariable("classId") Long classId, ModelMap mmp) {
        TeachingClasses classes = teachingClassesService.selectTeachingClassesByClassId(classId);
        mmp.put("teachingClasses", classes);
        return prefix + "/preLesson";
    }

    @GetMapping("/notice")
    public String notice() {
        return prefix + "/notice";
    }


    /**
     * 判断上课状态
     *
     * @param classId
     * @return
     */
    @PostMapping("/lessonStatus/{classId}")
    @ResponseBody
    public AjaxResult getLessonStatus(@PathVariable("classId") Long classId) {
        TeachingClasses classes = teachingClassesService.selectTeachingClassesByClassId(classId);
        return success(classes.getLessonStatus());
//        if (classes.getLessonStatus().equals("0")) {
//            return  AjaxResult.warn("班级正在上课中");
//        }
//        return success();
    }

}
